package com.example.forum2.Controller;

import com.example.forum2.Service.impl.IBoardServiceImpl;
import com.example.forum2.Service.impl.UserServiceImpl;
import com.example.forum2.comment.AppResult;
import com.example.forum2.comment.ResultCode;
import com.example.forum2.exception.ApplicationExeception;
import com.example.forum2.model.Board;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/board")
public class BoardController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private IBoardServiceImpl boardService;
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;
    @RequestMapping("/topList")
    public AppResult<List<Board>> topList (){
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        return AppResult.success(boards);
    }
    @RequestMapping("/getById")
    public AppResult<Board> getById(Long id){
        Board board = boardService.selectById(id);
        if(board==null||board.getDeleteState()==1){
            throw  new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        return AppResult.success(board);
    }

    @RequestMapping("/allNormal")
    public AppResult<List<Board>> allNormal () {
        // 调⽤Service层获取版块信息
        List<Board> boards = boardService.selectAllNormal();
        // 返回结果
        return AppResult.success(boards);
    }

    @RequestMapping("/create")
    public AppResult create (Integer sort,  String name) {
      // name="ddddd";
        Board board = new Board();
        board.setName(name);
        board.setSort(sort);
        boardService.create(board);
        return AppResult.success();
    }

}
